/*
 * @Author: xiaosihan
 * @Date: 2023-09-25 00:35:48
 * @Last Modified by: xiehangyunn
 * @Last Modified time: 2025-04-17 19:10:57
 */

import { MyPhotoData } from '#/type';
import userImageService from '@/api/services/userImageService';
import utilsServer from '@/api/services/utilsServer';
import designTemplateEditor from '@/pages/design/list/DesignTemplateEditor/designTemplateEditorer';
import utils from '@/utils/utils';
import { configure, observable, toJS } from 'mobx';

configure({ enforceActions: 'never' });

const myPhotoStore = (window.myPhotoStore = observable({
  // 当前是第几页
  page: 1,
  // 加载状态
  loading: false,
  setLoading(loading: boolean) {
    this.loading = loading;
  },

  //编辑中的文件夹id
  editorFolderId: 0,
  setEditorFolderId(editorFolderId: number) {
    this.editorFolderId = editorFolderId;
  },

  //选中的文件夹id
  activeFolderId: '' as number | '',
  setActiveFolderId(activeFolderId: number | '') {
    this.activeFolderId = activeFolderId;
  },

  //排列方式
  arrangeType: parseInt(localStorage.my_photo_page_size) || (1 as 1 | 2),
  setArrangeType(arrangeType: 1 | 2) {
    this.arrangeType = arrangeType;
    localStorage.my_photo_page_size = arrangeType;
  },

  // 是否还有下一页
  hasNext: true,
  setHasNext(hasNext: boolean) {
    this.hasNext = hasNext;
  },

  //上传中的状态
  uploadding: false,
  setUploadding(uploadding: boolean) {
    this.uploadding = uploadding;
  },

  //图片列表
  imageList: [] as Array<MyPhotoData>,
  imageListLength: 0,
  setImageList(imageList: Array<MyPhotoData>) {
    this.imageList = imageList;
    this.imageListLength = imageList.length;
  },

  //获取图片列表
  async getImageList(reset: boolean = false) {
    if (!this.loading && (reset || this.hasNext)) {
      this.setLoading(true);
      const res = await userImageService.get_user_image_list({
        pageNo: reset ? 0 : this.page++,
        pageSize: 10,
      });
      // 判断是否有下一页
      this.setHasNext(res.list.length === 10);

      if (reset) {
        this.setImageList(res.list as Array<MyPhotoData>);
      } else {
        this.setImageList([...toJS(this.imageList), ...res.list] as Array<MyPhotoData>);
      }
      this.setLoading(false);
    }
  },

  // 上传图片
  async upload() {
    this.setUploadding(true);
    const files = await utils.selectFiles();
    // for (let file of files) {
    //   const url = await utilsServer.uploadOss(file);
    //   designTemplateEditor.addImg("我的图片", utils.ossSuffix(url, 500));
    // }
    for (let file of files) {
      const url = await utilsServer.uploadOss(file).catch(() => { });
      if (url) {
        await userImageService.saveImage(url).catch(() => { });
      }
    }
    this.getImageList(true);
    this.setUploadding(false);
  },

  //直接通过url添加图片
  async uploadUrl(url: string) {
    this.setUploadding(true);
    // 图片保存的信息
    const saveInfo = await userImageService.saveImage(url);
    const newImageList = [{
      createTime: "",
      id: new Date().valueOf(),
      name: "",
      picPath: url,
      updateTime: "",
      userId: new Date().valueOf(),
    }, ...toJS(this.imageList)];
    // newImageList.unshift({ id: newImageid, url: url });
    this.setImageList(newImageList);
    this.setUploadding(false);
  },

  //删除一张图片
  async delImg(id: number) {
    userImageService
      .del_user_image_by_id(id)
      .then((res) => {
        let imageList = toJS(this.imageList);
        imageList = imageList.filter((img) => img.id !== id);
        this.setImageList(imageList);
      })
      .catch((err) => { });
  },
}));

export default myPhotoStore;
